# 获取当前使用的网卡
function Get-ActiveNetAdapter
{
    # 获取所有网卡
    $adapters = Get-NetAdapter

    # 找到当前使用的网卡
    $activeAdapter = $adapters | Where-Object { $_.Status -eq "Up" }

    # 返回当前使用的网卡
    return $activeAdapter
}
$activeAdapter = Get-ActiveNetAdapter

if ($activeAdapter)
{
    Write-Output "当前使用的网卡："
    Write-Output $activeAdapter.Name
}
else
{
    Write-Output "未找到当前使用的网卡"
}

# 禁用网卡
function Disable-NetAdapterByName {
    # 参数：网卡名称
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $Name
    )

    # 获取指定名称的网卡
    $adapter = Get-NetAdapter -Name $Name

    # 如果找到网卡，则禁用它
    if ($adapter) {
        Disable-NetAdapter -Name $Name -Confirm:$false
        Write-Output "网卡 $Name 已禁用"
    } else {
        Write-Output "未找到名称为 $Name 的网卡"
    }
}
function Enable-NetAdapterByName {
    # 参数：网卡名称
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $Name
    )

    # 获取指定名称的网卡
    $adapter = Get-NetAdapter -Name $Name

    # 如果找到网卡，则启用它
    if ($adapter) {
        Enable-NetAdapter -Name $Name -Confirm:$false
        Write-Output "网卡 $Name 已启用"
    } else {
        Write-Output "未找到名称为 $Name 的网卡"
    }
}

# 禁用网卡net1 启用网卡net2
Disable-NetAdapterByName -Name "net1"
Enable-NetAdapterByName -Name "net2"

function Get-ActiveNetAdapterInfo {
    # 获取所有网卡
    $adapters = Get-NetAdapter
    # 筛选已连接的网卡
    $activeAdapters = $adapters | Where-Object { $_.Status -eq "Up" }
    # 遍历每个已连接的网卡
    foreach ($adapter in $activeAdapters) {
        Write-Output "当前启用的网卡:"
        Write-Output "名称:" $adapter.Name
        Write-Output "状态:" $adapter.Status
        Write-Output "-------------------------"
    }
}
Get-ActiveNetAdapterInfo



$currentAdapterName = "net2"
function Get-CurrentAdapterSpeed
{
    param([string]$adapterName)
    $adapter = Get-NetAdapterStatistics -Name $adapterName
    return @{ ReceivedSpeed = [Math]::Round($adapter.ReceivedBytes / 1024); SentSpeed = [Math]::Round($adapter.SentBytes / 1024) }
}

$speedResult = Get-CurrentAdapterSpeed -adapterName $currentAdapterName
$receivedSpeed = $result.ReceivedSpeed
$sentSpeed = $result.SentSpeed
Write-Output $speedResult

# 初始化状态
$thresholdSpeed = 1024  # 1M/s in kb/s
$speedCheckDuration = 5
$maxLowSpeedDurationCount = 3  # 15 seconds at 5s interval
$speedBelowThresholdCount = 0
$state = "WaitingToReceiveFile"


function Get-AverageAdapterSpeed
{
    # 参数：
    # - adapterName: 网卡名称
    # - interval: 速度检查间隔 (秒)，默认为 5 秒
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $adapterName,

        [Parameter(Mandatory=$false)]
        [int]
        $interval = 5
    )

    # 初始化变量
    $startTime = Get-Date
    $endTime = $startTime + (New-TimeSpan -Seconds $interval)

    # 获取开始时网卡统计信息
    $startStats = Get-NetAdapterStatistics -Name $adapterName

    # 等待五秒钟
    Start-Sleep -Seconds $interval

    # 获取结束时网卡统计信息
    $endStats = Get-NetAdapterStatistics -Name $adapterName

    # 计算平均速率
    $totalReceivedBytes = $endStats.ReceivedBytes - $startStats.ReceivedBytes
    $totalSentBytes = $endStats.SentBytes - $startStats.SentBytes

    $averageReceivedSpeed = [Math]::Round($totalReceivedBytes / ($interval) / 1024)
    $averageSentSpeed = [Math]::Round($totalSentBytes / ($interval) / 1024)

    # 返回结果
    return @{
        ReceivedSpeed = $averageReceivedSpeed;
        SentSpeed = $averageSentSpeed;
    }
}


# 计算 net2 网卡的平均网速
$averageSpeed = Get-AverageAdapterSpeed -adapterName "net2"

Write-Output "平均接收速度:" $averageSpeed.ReceivedSpeed "kb/s"
Write-Output "平均发送速度:" $averageSpeed.SentSpeed "kb/s"
